﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HOOMT
{
    public partial class OriginalModelForm : Form
    {
        #region 属性
        protected MainForm parentMainForm = null;
        public List<OriginalDiagram> diagramList = new List<OriginalDiagram>();//存放图形对象
        public List<OriginalLine> straightlineList = new List<OriginalLine>();//存放线条
        //public string formNodeFullPath = "Untitled";
        public static Point locationPoint = new Point(0,0);
        public String id;

        //选中
        protected OriginalDiagram selectedDiagram = null;//保存当前选中图形对象
        protected OriginalDiagram preSelectedDiagram = null;
        protected OriginalLine selectedLine = null;
        //移动
        protected bool movingDiagramFlag = false;//图形移动标志
        protected Point movingUpLeftPoint = new Point();
        protected Point movingMousePoint = new Point();
        //缩放
        protected bool magnifyingDiagramFlag = false;//图形缩放标志
        protected Point magnifyingFixedPoint = new Point();
        protected Point magnifyingMousePoint = new Point();
        protected int magnifyingPointFlag;//缩放点（左上(0)右上(1)右下(2)左下(3)）
        //画线
        protected bool drawingLineFlag = false;//连线是否有效（起点是否在一个图形对象内）
        protected Point startPoint, endPoint; //用于画图形对象的连接线
        protected Point dynamicStartPoint, dynamicEndPoint;//用于画弹性线
        protected OriginalDiagram lineStartDiagram, lineEndDiagram;//记录连线的起始和终止图形对象，方便向其linePointList加入点
        //treeView
        //public TreeNode preTreeNode = null;
        //private string formNodeFullPath;
        protected int highObjectDiagramNum = 0;//记录HighOrderObjectDiagram个数
        protected int PrimitiveObjectDiagramNum = 0;

        protected int highStateDiagramNum = 0;//记录HighStateDiagram个数
        protected int PrimitiveStateNum = 0;

        protected int highInformationDiagramNum = 0;//记录InformationSourceDiagram个数
        protected int processDiagramNum = 0;
        #endregion

        #region 构造函数
        public OriginalModelForm()
        {
            InitializeComponent();
            this.id = System.Guid.NewGuid().ToString();

          //  Console.WriteLine(id);
        }
        #endregion

        #region 窗口属性改变函数集
        //单击窗口，使其显示在最上层，且显示相应工具栏
        protected virtual void ModelForm_Click(object sender, EventArgs e)
        {
            
        }
        //关闭窗体事件，实则是隐藏窗体
        protected virtual void ModelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
        //处理窗体最大化、最小化、改变大小时的工具栏显示问题
        protected virtual void ModelForm_Resize(object sender, EventArgs e)
        {
            
        }
        //窗口初始化
        private void ModelForm_Load(object sender, EventArgs e)
        {
            this.Location = locationPoint;
            //改变窗口出现的位置
            locationPoint.X += 20;
            locationPoint.Y += 35;
        }
        #endregion

        #region 图形对象重绘函数
        //
        private void ModelForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = CreateGraphics();
            //平滑效果
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //遍历图形对象列表
      //      Console.WriteLine("aaaaa"+diagramList.Count);
            foreach (OriginalDiagram diag in diagramList)
            {
                //绘制图形
                diag.Drawing(g, this);
                //绘制以图形为起点对象的直线
                foreach (OriginalLine line in diag.AssociationStartList)
                {
                    //选中则绘制直线选中框
                    if (line.IsSelected)
                    {
                        Pen penSolid = new Pen(Color.Black, 1);
                        int recWidth = 5;
                        Rectangle[] recArray = new Rectangle[2]
                    { 
                        new Rectangle(line.GetStartShowPoint().X - recWidth, line.GetStartShowPoint().Y - recWidth, recWidth, recWidth),
                        new Rectangle(line.GetEndShowPoint().X,line.GetEndShowPoint().Y, recWidth, recWidth)
                    };
                        g.DrawRectangles(penSolid, recArray);
                        g.FillRectangles(new SolidBrush(Color.Black), recArray);
                    }
                    line.Drawing(g, this);
                }
                //选中则绘制图形选中框
                if (diag.IsSelected)
                {
                    Pen penSolid = new Pen(Color.Black, 1);
                    int recWidth = 5;
                    Rectangle[] recArray = new Rectangle[4]
                        { 
                        new Rectangle(diag.UpLeftpoint.X - recWidth, diag.UpLeftpoint.Y - recWidth, recWidth, recWidth),
                        new Rectangle(diag.UpLeftpoint.X + diag.Width, diag.UpLeftpoint.Y - recWidth, recWidth, recWidth),
                        new Rectangle(diag.UpLeftpoint.X - recWidth, diag.UpLeftpoint.Y + diag.Height, recWidth, recWidth),
                        new Rectangle(diag.UpLeftpoint.X + diag.Width, diag.UpLeftpoint.Y + diag.Height, recWidth, recWidth)
                        };
                    g.DrawRectangles(penSolid, recArray);
                    g.FillRectangles(new SolidBrush(Color.Black), recArray);
                }  
            }
            //遍历直线列表
            /*foreach (OriginalLine line in straightlineList)
            {
                if (line.IsSelected)
                {
                    Pen penSolid = new Pen(Color.Black, 1);
                    int recWidth = 5;
                    Rectangle[] recArray = new Rectangle[2]
                    { 
                        new Rectangle(line.GetStartShowPoint().X - recWidth, line.GetStartShowPoint().Y - recWidth, recWidth, recWidth),
                        new Rectangle(line.GetEndShowPoint().X,line.GetEndShowPoint().Y, recWidth, recWidth)
                    };
                    g.DrawRectangles(penSolid, recArray);
                    g.FillRectangles(new SolidBrush(Color.Black), recArray);
                }
                line.Drawing(g, this);
            }*/
        }
        #endregion

        #region 图形对象键盘响应函数
        //
        private void ModelForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                bool nameBoxFocused = false;
                foreach (OriginalDiagram diag in diagramList)
                {
                    if (diag.NameBox.Focused)
                    {
                        nameBoxFocused = true;
                        break;
                    }
                }
                //若有对象的NameBox获得焦点，则只能删除文字，不能删除对象或线条
                if (!nameBoxFocused)
                {
                    foreach (OriginalDiagram diag in diagramList)
                    {
                        if (diag.IsSelected)
                        {
                            //Point centerPoint = new Point(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + diag.Height / 2);
                            this.Controls.Remove(diag.NameBox);
                            diagramList.Remove(diag);
                            //删除与此图形对象相连接的所有线条
                            /*for (int i = 0; i < straightlineList.Count(); i++)
                            {
                                if (straightlineList[i].StartPoint == centerPoint || straightlineList[i].EndPoint == centerPoint)
                                {
                                    straightlineList.Remove(straightlineList[i]);
                                    i--;//调整位置
                                }
                            }*/
                            //diag.AssociationStartList.Clear();
                            //删除与选中图像对象有关的直线的所以引用(存在三个副本，都要删除)
                            foreach (OriginalLine line in diag.AssociationStartList)
                            {
                                straightlineList.Remove(line);
                                line.EndDiagram.AssociationEndList.Remove(line);
                            }
                            foreach (OriginalLine line in diag.AssociationEndList)
                            {
                                straightlineList.Remove(line);
                                line.StartDiagram.AssociationStartList.Remove(line);
                            }
                            Invalidate();
                            break;
                        }
                    }
                    //删除选中直线
                    foreach (OriginalLine line in straightlineList)
                    {
                        if (line.IsSelected)
                        {
                            line.StartDiagram.AssociationStartList.Remove(line);
                            line.EndDiagram.AssociationEndList.Remove(line);
                            straightlineList.Remove(line);
                            Invalidate(/*rect*/);
                            break;
                        }
                    }
                }
            }
        }
        #endregion

        #region 图形对象鼠标响应函数集
        //
        protected virtual void ModelForm_MouseEnter(object sender, EventArgs e)
        {

        }
        //
        protected virtual void ModelForm_MouseDown(object sender, MouseEventArgs e)
        {
           
        }
        //
        protected virtual void ModelForm_MouseMove(object sender, MouseEventArgs e)
        {
            
        }
        //
        protected virtual void ModelForm_MouseUp(object sender, MouseEventArgs e)
        {
            movingDiagramFlag = false;
            magnifyingDiagramFlag = false;
            drawingLineFlag = false;
        }
        #endregion

        #region 内部函数
        //添加图形对象
        protected void AddDiagram(Diagram dgm, Point pt)
        {
            //HOOM
            if (dgm == Diagram.HighOrderObject)
            {
                selectedDiagram = new HighOrderObjectDiagram(pt);
                if (highObjectDiagramNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + highObjectDiagramNum;//修改初始名字（编号）
                }
                selectedDiagram.highDiagramNum = ++highObjectDiagramNum;//给对象赋序号
                //AddTreeViewNode();
                /*TreeNode[] mainNode = new TreeNode[1];//构建节点
                mainNode[0] = new TreeNode("Associations");
                TreeNode node = new TreeNode(selectedDiagram.Name, mainNode);
                node.Tag = selectedDiagram;//节点指向对象(用于删除节点时同步删除对象)
                selectedDiagram.node = node;//对象指向节点
                this.preTreeNode.Nodes.Add(node);//增加节点
                node.Expand();//展开
                node.ContextMenuStrip = parentMainForm.nodeMenuStrip;//为节点添加右键响应菜单*/
                //this.preTreeNode.Nodes[selectedDiagram.highDiagramNum + 1].Expand();//展开
                //this.preTreeNode.Nodes[selectedDiagram.highDiagramNum + 1].ContextMenuStrip = parentMainForm.nodeMenuStrip;//为节点添加右键响应菜单
                //selectedDiagram.diagramNodeFullPath = this.preTreeNode.FullPath + "\\" + selectedDiagram.Name; //+ "\\" + selectedDiagram.Name;//设置FullPath
                
            }
            else if (dgm == Diagram.PrimitiveObject)
            {
                selectedDiagram = new PrimitiveObjectDiagram(pt);
                if (PrimitiveObjectDiagramNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + PrimitiveObjectDiagramNum;//修改初始名字（编号）
                }
                ++PrimitiveObjectDiagramNum;
            }
            //HSTM
            else if (dgm == Diagram.HighState)
            {
                selectedDiagram = new HighStateDiagram(pt);
                if (highStateDiagramNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + highStateDiagramNum;//修改初始名字（编号）
                }
                selectedDiagram.highDiagramNum = ++highStateDiagramNum;//给对象赋序号
                //AddTreeViewNode();
                /*TreeNode[] mainNode = new TreeNode[1];//构建节点
                mainNode[0] = new TreeNode("Associations");
                TreeNode node = new TreeNode(selectedDiagram.Name, mainNode);
                node.Tag = selectedDiagram;//节点指向对象(用于删除节点时同步删除对象)
                selectedDiagram.node = node;//对象指向节点
                this.preTreeNode.Nodes.Add(node);//增加节点
                node.Expand();//展开
                node.ContextMenuStrip = parentMainForm.nodeMenuStrip;//为节点添加右键响应菜单*/
            }
            else if( dgm == Diagram.PrimitiveState)
            {
                selectedDiagram = new PrimitiveStateDiagram(pt);
                if (PrimitiveStateNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + PrimitiveStateNum;
                }
                ++PrimitiveStateNum;
            }
            //HOIFM
            else if(parentMainForm.currentDiagramFlag == Diagram.InformationSource)
            {
                selectedDiagram = new InformationSourceDiagram(pt);
                if (highInformationDiagramNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + highInformationDiagramNum;//修改初始名字（编号）
                }
                selectedDiagram.highDiagramNum = ++highInformationDiagramNum;//给对象赋序号
                //AddTreeViewNode();
                /*TreeNode[] mainNode = new TreeNode[1];//构建节点
                mainNode[0] = new TreeNode("Associations");
                TreeNode node = new TreeNode(selectedDiagram.Name, mainNode);
                node.Tag = selectedDiagram;//节点指向对象(用于删除节点时同步删除对象)
                selectedDiagram.node = node;//对象指向节点
                this.preTreeNode.Nodes.Add(node);//增加节点
                node.Expand();//展开
                node.ContextMenuStrip = parentMainForm.nodeMenuStrip;//为节点添加右键响应菜单*/
            }
            else if (parentMainForm.currentDiagramFlag == Diagram.Process)
            {
                selectedDiagram = new ProcessDiagram(pt);
                if (processDiagramNum != 0)
                {
                    selectedDiagram.Name = selectedDiagram.Name + processDiagramNum;
                }
                ++processDiagramNum;
            }

       //     selectedDiagram.Id = (System.Guid.NewGuid()).ToString();//给新增图形对象赋ID号
            preSelectedDiagram = selectedDiagram;
            
            diagramList.Add(selectedDiagram);
            selectedDiagram.ModelForm = this;//设置对象所在的绘制载体

            Cursor = System.Windows.Forms.Cursors.Arrow;
            Invalidate(/*new Rectangle(currentDiagram.UpLeftpoint.X, currentDiagram.UpLeftpoint.Y, currentDiagram.Width, currentDiagram.Height)*/);
            parentMainForm.currentDiagramFlag = Diagram.NullObject;
        }
        private void AddTreeViewNode()
        {
            //TreeNode[] mainNode = new TreeNode[1];//构建节点
            //mainNode[0] = new TreeNode("Associations");
            //TreeNode node = new TreeNode(selectedDiagram.Name, mainNode);
            //node.Tag = selectedDiagram;//节点指向对象(用于删除节点时同步删除对象)
            //selectedDiagram.treeNode = node;//对象指向节点
            //this.preTreeNode.Nodes.Add(node);//增加节点
            //node.Expand();//展开
            //node.ContextMenuStrip = parentMainForm.nodeMenuStrip;//为节点添加右键响应菜单
        }
        //选中图形对象
        protected void SelectDiagram(Point pt)
        {
            foreach (OriginalDiagram diag in diagramList)
            {
                Rectangle rect = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height);
                if (rect.Contains(pt))
                {
                    diag.IsSelected = true;
                    selectedDiagram = diag;
                    preSelectedDiagram = diag;
                    movingDiagramFlag = true;
                    //magnifyingDiagramFlag = true;
                    movingMousePoint = new Point(pt.X, pt.Y);
                    movingUpLeftPoint = selectedDiagram.UpLeftpoint;
                    break;
                }
                else
                {
                    diag.IsSelected = false;
                    //selectedDiagram = null;
                }
            }
            Invalidate();
        }
        //选中线条
        protected void SelecteLine(Point pt)
        {
            foreach (OriginalLine line in straightlineList)
            {
                if (line.IsSelectedLine(pt, line.StartPoint, line.EndPoint) && (selectedDiagram == null))//对象的选中权优先于直线，且不能同时选中
                {
                    line.IsSelected = true;
                    selectedLine = line;
                    Invalidate(/*new Rectangle(selectedLine.UpLeftpoint.X - 5, selectedLine.UpLeftpoint.Y - 5, selectedLine.Width + 11, selectedLine.Height + 11)*/);
                    break;
                }
                else
                {
                    line.IsSelected = false;
                }
            }
        }
        //图形对象移动过程
        protected void MovingDiagram(OriginalDiagram dgm, Point pt)
        {
            int tempX = movingMousePoint.X - pt.X;
            int tempY = movingMousePoint.Y - pt.Y;
            movingMousePoint.X = pt.X;
            movingMousePoint.Y = pt.Y;

            int n = movingUpLeftPoint.X;
            int m = movingUpLeftPoint.Y;
            movingUpLeftPoint.X -= tempX;
            movingUpLeftPoint.Y -= tempY;

            Graphics g = CreateGraphics();
            Pen penDot = new Pen(Color.Black, 1);
            penDot.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            //局部更新
            Invalidate(new Rectangle(n, m, selectedDiagram.Width + 1, selectedDiagram.Height + 1));
            g.DrawRectangle(penDot, movingUpLeftPoint.X, movingUpLeftPoint.Y, selectedDiagram.Width, selectedDiagram.Height);
        }
        //图形对象移动结果
        protected void MovedDiagram(OriginalDiagram dgm, Point pt)
        {
            Invalidate(new Rectangle(selectedDiagram.UpLeftpoint.X - 5, selectedDiagram.UpLeftpoint.Y - 5, selectedDiagram.Width + 11, selectedDiagram.Height + 11));
            Point centerPoint1 = new Point(selectedDiagram.UpLeftpoint.X + selectedDiagram.Width / 2, selectedDiagram.UpLeftpoint.Y + selectedDiagram.Height / 2);
            selectedDiagram.UpLeftpoint = movingUpLeftPoint;
            Point centerPoint2 = new Point(selectedDiagram.UpLeftpoint.X + selectedDiagram.Width / 2, selectedDiagram.UpLeftpoint.Y + selectedDiagram.Height / 2);
            Invalidate(new Rectangle(selectedDiagram.UpLeftpoint.X - 5, selectedDiagram.UpLeftpoint.Y - 5, selectedDiagram.Width + 11, selectedDiagram.Height + 11));
            //selectedDiagram = selectedDiagram;
            foreach(OriginalLine line in selectedDiagram.AssociationStartList)
            {
                line.StartPoint = centerPoint2;//更改开始关联线段的起点
            }
            foreach (OriginalLine line in selectedDiagram.AssociationEndList)
            {
                line.EndPoint = centerPoint2;//更改终止关联线段的终点
            }
            selectedDiagram = null;
            //更改与移动后对象相连的直线端点
            /*foreach (OriginalLine line in straightlineList)
            {
                if (line.StartPoint == centerPoint1)
                {
                    line.StartPoint = centerPoint2;
                    //line.UpLeftpoint = new Point(Math.Min(line.StartPoint.X, line.EndPoint.X), Math.Min(line.StartPoint.Y, line.EndPoint.Y));
                    //line.Width = Math.Abs(line.StartPoint.X - line.EndPoint.X);
                    //line.Height = Math.Abs(line.StartPoint.Y - line.EndPoint.Y);
                }
                else if (line.EndPoint == centerPoint1)
                {
                    line.EndPoint = centerPoint2;
                    //line.UpLeftpoint = new Point(Math.Min(line.StartPoint.X, line.EndPoint.X), Math.Min(line.StartPoint.Y, line.EndPoint.Y));
                    //line.Width = Math.Abs(line.StartPoint.X - line.EndPoint.X);
                    //line.Height = Math.Abs(line.StartPoint.Y - line.EndPoint.Y);
                }
            }*/
            //全局更新，可以优化！！！！！！！！！
            Invalidate();
        }
        //缩放准备
        protected void selectedMagnifyingDiagramPoint(Point pt)
        {
            Pen penSolid = new Pen(Color.Black, 1);
            int recWidth = 5;
            Rectangle[] recArray = new Rectangle[4]
                        { 
                        new Rectangle(preSelectedDiagram.UpLeftpoint.X - recWidth, preSelectedDiagram.UpLeftpoint.Y - recWidth, recWidth, recWidth),
                        new Rectangle(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width, preSelectedDiagram.UpLeftpoint.Y - recWidth, recWidth, recWidth),
                        new Rectangle(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height, recWidth, recWidth),
                        new Rectangle(preSelectedDiagram.UpLeftpoint.X - recWidth, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height, recWidth, recWidth)
                        };
            #region 取点
            //左上点
            if (recArray[0].Contains(pt))
            {
                magnifyingPointFlag = 0;
                magnifyingDiagramPre(new Point(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height), pt);
                //new Point(selectedDiagram.UpLeftpoint.X, selectedDiagram.UpLeftpoint.Y));
            }
            //右上点
            else if (recArray[1].Contains(pt))
            {
                magnifyingPointFlag = 1;
                magnifyingDiagramPre(new Point(preSelectedDiagram.UpLeftpoint.X, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height), pt);
                //new Point(selectedDiagram.UpLeftpoint.X + selectedDiagram.Width, selectedDiagram.UpLeftpoint.Y));
            }
            //右下点
            else if (recArray[2].Contains(pt))
            {
                magnifyingPointFlag = 2;
                magnifyingDiagramPre(new Point(preSelectedDiagram.UpLeftpoint.X, preSelectedDiagram.UpLeftpoint.Y), pt);
                //new Point(selectedDiagram.UpLeftpoint.X + selectedDiagram.Width, selectedDiagram.UpLeftpoint.Y + selectedDiagram.Height));
            }
            //左下点
            else if (recArray[3].Contains(pt))
            {
                magnifyingPointFlag = 3;
                magnifyingDiagramPre(new Point(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width, preSelectedDiagram.UpLeftpoint.Y), pt);
                //new Point(selectedDiagram.UpLeftpoint.X, selectedDiagram.UpLeftpoint.Y + selectedDiagram.Height));
            }
            #endregion
        }
        protected void magnifyingDiagramPre(Point FixedPoint, Point pt)
        {
            magnifyingFixedPoint = FixedPoint;
            magnifyingMousePoint = pt;
            magnifyingDiagramFlag = true;
        }
        //图形对象缩放过程
        protected void magnifyingDiagram(Point pt)
        {
            Graphics g = CreateGraphics();
            Pen penDot = new Pen(Color.Black, 1);
            penDot.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            //移动坐上点
            if (magnifyingPointFlag == 0)
            {
                //魔数为调整刷新时的矩形范围
                if (magnifyingMousePoint.X >= pt.X && magnifyingMousePoint.Y >= pt.Y)
                {
                    Invalidate(new Rectangle(pt.X + 1, pt.Y + 1, magnifyingFixedPoint.X - pt.X - 1, magnifyingFixedPoint.Y - pt.Y - 1));
                }
                else if (magnifyingMousePoint.X == pt.X)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 2, magnifyingMousePoint.Y - 2, magnifyingFixedPoint.X - magnifyingMousePoint.X + 3, 3));
                }
                else if (magnifyingMousePoint.Y == pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 2, magnifyingMousePoint.Y - 2, 3, magnifyingFixedPoint.Y - magnifyingMousePoint.Y + 3));
                }
                else
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 2, magnifyingMousePoint.Y - 2, magnifyingFixedPoint.X - magnifyingMousePoint.X + 3, 3));
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 2, magnifyingMousePoint.Y - 2, 3, magnifyingFixedPoint.Y - magnifyingMousePoint.Y + 3));
                }
                g.DrawRectangle(penDot, pt.X, pt.Y, magnifyingFixedPoint.X - pt.X, magnifyingFixedPoint.Y - pt.Y);
                magnifyingMousePoint = new Point(pt.X, pt.Y);
            }
            //移动右上点
            else if (magnifyingPointFlag == 1)
            {
                if (magnifyingMousePoint.X <= pt.X && magnifyingMousePoint.Y >= pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X + 1, magnifyingMousePoint.Y, magnifyingMousePoint.X - magnifyingFixedPoint.X, magnifyingFixedPoint.Y - magnifyingMousePoint.Y));
                }
                else if (magnifyingMousePoint.X == pt.X)
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X - 2, magnifyingMousePoint.Y - 2, magnifyingMousePoint.X - magnifyingFixedPoint.X + 3, 3));
                }
                else if (magnifyingMousePoint.Y == pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X, magnifyingMousePoint.Y, 3, magnifyingFixedPoint.Y - magnifyingMousePoint.Y + 3));
                }
                else
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X - 2, magnifyingMousePoint.Y - 2, magnifyingMousePoint.X - magnifyingFixedPoint.X + 3, 3));
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 2, magnifyingMousePoint.Y - 2, 3, magnifyingFixedPoint.Y - magnifyingMousePoint.Y + 3));
                }

                g.DrawRectangle(penDot, magnifyingFixedPoint.X, pt.Y, pt.X - magnifyingFixedPoint.X, magnifyingFixedPoint.Y - pt.Y);
                magnifyingMousePoint = new Point(pt.X, pt.Y);
            }
            //移动右下点
            else if (magnifyingPointFlag == 2)
            {
                if (magnifyingMousePoint.X <= pt.X && magnifyingMousePoint.Y <= pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X + 1, magnifyingFixedPoint.Y + 1, magnifyingMousePoint.X - magnifyingFixedPoint.X - 1, magnifyingMousePoint.Y - magnifyingFixedPoint.Y - 1));
                }
                else if (magnifyingMousePoint.X == pt.X)
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X, magnifyingMousePoint.Y, magnifyingMousePoint.X - magnifyingFixedPoint.X + 3, 3));
                }
                else if (magnifyingMousePoint.Y == pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X, magnifyingFixedPoint.Y, 3, magnifyingMousePoint.Y - magnifyingFixedPoint.Y + 3));
                }
                else
                {
                    Invalidate(new Rectangle(magnifyingFixedPoint.X - 1, magnifyingMousePoint.Y - 1, magnifyingMousePoint.X - magnifyingFixedPoint.X + 3, 3));
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 1, magnifyingFixedPoint.Y - 1, 3, magnifyingMousePoint.Y - magnifyingFixedPoint.Y + 3));
                }

                g.DrawRectangle(penDot, magnifyingFixedPoint.X, magnifyingFixedPoint.Y, pt.X - magnifyingFixedPoint.X, pt.Y - magnifyingFixedPoint.Y);
                magnifyingMousePoint = new Point(pt.X, pt.Y);
            }
            //移动左下点
            else if (magnifyingPointFlag == 3)
            {
                if (magnifyingMousePoint.X >= pt.X && magnifyingMousePoint.Y <= pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X + 1, magnifyingFixedPoint.Y + 1, magnifyingFixedPoint.X - magnifyingMousePoint.X - 1, magnifyingMousePoint.Y - magnifyingFixedPoint.Y - 1));
                }
                else if (magnifyingMousePoint.X == pt.X)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X, magnifyingMousePoint.Y, magnifyingFixedPoint.X - magnifyingMousePoint.X + 3, 3));
                }
                else if (magnifyingMousePoint.Y == pt.Y)
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 1, magnifyingFixedPoint.Y - 1, 2, magnifyingMousePoint.Y - magnifyingFixedPoint.Y + 2));
                }
                else
                {
                    Invalidate(new Rectangle(magnifyingMousePoint.X, magnifyingMousePoint.Y, magnifyingFixedPoint.X - magnifyingMousePoint.X + 3, 3));
                    Invalidate(new Rectangle(magnifyingMousePoint.X - 1, magnifyingFixedPoint.Y - 1, 2, magnifyingMousePoint.Y - magnifyingFixedPoint.Y + 2));
                }

                g.DrawRectangle(penDot, pt.X, magnifyingFixedPoint.Y, magnifyingFixedPoint.X - pt.X, pt.Y - magnifyingFixedPoint.Y);
                magnifyingMousePoint = new Point(pt.X, pt.Y);
            }
        }
        //图形对象缩放结果
        protected void magnifiedDiagram(Point pt)
        {
            Point centerPoint1 = new Point(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width / 2, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height / 2);
            if (magnifyingPointFlag == 0)
            {
                preSelectedDiagram.UpLeftpoint = new Point(pt.X, pt.Y);
                preSelectedDiagram.Width = magnifyingFixedPoint.X - pt.X;
                preSelectedDiagram.Height = magnifyingFixedPoint.Y - pt.Y;
            }
            else if (magnifyingPointFlag == 1)
            {
                preSelectedDiagram.UpLeftpoint = new Point(magnifyingFixedPoint.X, pt.Y);
                preSelectedDiagram.Width = pt.X - magnifyingFixedPoint.X;
                preSelectedDiagram.Height = magnifyingFixedPoint.Y - pt.Y;
            }
            else if (magnifyingPointFlag == 2)
            {
                //preSelectedDiagram.UpLeftpoint = new Point(e.X, e.Y);
                preSelectedDiagram.Width = pt.X - magnifyingFixedPoint.X;
                preSelectedDiagram.Height = pt.Y - magnifyingFixedPoint.Y;
            }
            else if (magnifyingPointFlag == 3)
            {
                preSelectedDiagram.UpLeftpoint = new Point(pt.X, magnifyingFixedPoint.Y);
                preSelectedDiagram.Width = magnifyingFixedPoint.X - pt.X;
                preSelectedDiagram.Height = pt.Y - magnifyingFixedPoint.Y;
            }
            preSelectedDiagram.Drawing(CreateGraphics(), this);//刷新图形对象的最少高度
            //更新缩放对象的连线位置
            Point centerPoint2 = new Point(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width / 2, preSelectedDiagram.UpLeftpoint.Y + preSelectedDiagram.Height / 2);
            //int minHeight = preSelectedDiagram.getMinHeight();
            //Point centerMinHeight = new Point(preSelectedDiagram.UpLeftpoint.X + preSelectedDiagram.Width / 2, preSelectedDiagram.UpLeftpoint.Y + minHeight / 2);
            
            foreach(OriginalLine line in preSelectedDiagram.AssociationStartList)
            {
                line.StartPoint = centerPoint2;//更改开始关联线段的起点
                line.StartDiagramSize = new Size(preSelectedDiagram.Width / 2, preSelectedDiagram.Height / 2);
            }
            foreach (OriginalLine line in preSelectedDiagram.AssociationEndList)
            {
                line.EndPoint = centerPoint2;//更改终止关联线段的终点
                line.EndDiagramSize = new Size(preSelectedDiagram.Width / 2, preSelectedDiagram.Height / 2);
            }
            /*foreach (OriginalLine line in straightlineList)
            {
                //判断中心点是否是直线端点且高度不能小于图形对象最少高度(当图像对象内部显示文字时，有最少高度限制)
                if (line.StartPoint == centerPoint1)
                {
                    //if( preSelectedDiagram.Height > minHeight)
                    {
                        line.StartPoint = centerPoint2;
                        line.StartDiagramSize = new Size(preSelectedDiagram.Width / 2, preSelectedDiagram.Height / 2);
                    }
                    //else
                    {
                        //line.StartPoint = centerMinHeight;
                        //line.StartDiagramSize = new Size(preSelectedDiagram.Width / 2, minHeight / 2);
                    }
                }
                //
                if (line.EndPoint == centerPoint1 )
                {
                    //if (preSelectedDiagram.Height > minHeight)
                    {
                        line.EndPoint = centerPoint2;
                        line.EndDiagramSize = new Size(preSelectedDiagram.Width / 2, preSelectedDiagram.Height / 2);
                    }
                    //else
                    {
                        //line.EndPoint = centerMinHeight;
                        //line.EndDiagramSize = new Size(preSelectedDiagram.Width / 2, minHeight / 2);
                    }
                }  
            }*/
            preSelectedDiagram.IsSelected = true;
            selectedDiagram = preSelectedDiagram;
            Invalidate();
        }
        //画线准备
        protected void DrawingLinePre(Point pt)
        {
            foreach (OriginalDiagram diag in diagramList)
            {
                if (new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height).Contains(pt))
                {
                    startPoint = new Point(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + diag.Height / 2);
                    endPoint = startPoint;
                    drawingLineFlag = true;
                    lineStartDiagram = diag;
                    break;
                }
            }
            dynamicStartPoint = PointToScreen(startPoint);
            dynamicEndPoint = dynamicStartPoint;
        }
        //画线动态过程
        protected void DrawingLine(Point pt)
        {
            Graphics g = CreateGraphics();
            Pen penDot = new Pen(Color.Black, 1);
            penDot.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            ControlPaint.DrawReversibleLine(dynamicStartPoint, dynamicEndPoint, Color.Green);
            dynamicEndPoint = new Point(pt.X, pt.Y);
            dynamicEndPoint = PointToScreen(dynamicEndPoint);
            ControlPaint.DrawReversibleLine(dynamicStartPoint, dynamicEndPoint, Color.Green);
        }
        //画线结果(判断是否添加线条)
        protected void DrawedLine(Diagram dgm,Point pt)
        {
            ClearSelection();
            //判断点是否在一个对象上，否，则无效连接
            foreach (OriginalDiagram diag in diagramList)
            {
                if (new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height).Contains(pt))
                {
                    endPoint = new Point(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + diag.Height / 2);
                    lineEndDiagram = diag;
                    break;
                }
                else
                    endPoint = startPoint;
            }
            if (endPoint != startPoint)
            {
                OriginalLine currentLine = null;//new StaticSimpleRelationship(startPoint, endPoint);;
                switch (dgm)
                {
                    case Diagram.ComplexRelationship:
                        currentLine = new StaticComplexRelationship(startPoint, endPoint, lineStartDiagram, lineEndDiagram);
                        break;
                    case Diagram.SimpleRelationship:
                        currentLine = new StaticSimpleRelationship(startPoint, endPoint, lineStartDiagram, lineEndDiagram);
                        break;
                    case Diagram.StateTransitionFlow:
                        currentLine = new StateTransformFlow(startPoint, endPoint, lineStartDiagram, lineEndDiagram);
                        break;
                    case Diagram.InformationFlow:
                        currentLine = new InformationFlow(startPoint, endPoint, lineStartDiagram, lineEndDiagram);
                        break;
                        
                }
                
                //if (parentMainForm.currentDiagramFlag == Diagram.SimpleRelationship)
                {
                    //currentLine = new StaticSimpleRelationship(startPoint, endPoint);
                }
                bool equal = false;
                //保证无重复连接线
                foreach (OriginalLine line in straightlineList)
                {
                    if (currentLine.equalTo(line))
                    {
                        if (line is StaticSimpleRelationship && currentLine is StaticComplexRelationship)
                        {
                            //把存在的StaticSimpleRelationship关系替换为StaticComplexRelationship关系
                            straightlineList.Remove(line);
                            break;
                        }
                        else
                        {
                            equal = true;
                        }
                    }
                }
                if (!equal)
                {
                    //currentLine.StartDiagramSize = new Size(lineStartDiagram.Width / 2, lineStartDiagram.Height / 2);
                    //currentLine.EndDiagramSize = new Size(lineEndDiagram.Width / 2, lineEndDiagram.Height / 2);
                    //记录线条两端的关联图形对象，并把直线加入到开始的图形对象的关联直线列表(开始对象可以通过关联直线导航到被关联的图形对象)
                    //currentLine.StartDiagram = lineStartDiagram;
                    //currentLine.EndDiagram = lineEndDiagram;

                    currentLine.StartDiagram.AssociationStartList.Add(currentLine);
                    currentLine.EndDiagram.AssociationEndList.Add(currentLine);

                    straightlineList.Add(currentLine);
                }
                Cursor = System.Windows.Forms.Cursors.Arrow;
                Invalidate(/*new Rectangle(Math.Min(startPoint.X, pt.X), Math.Min(startPoint.Y, pt.Y), Math.Abs(startPoint.X - pt.X), Math.Abs(startPoint.Y - pt.Y))*/);
                //selectedLine = currentLine;
                parentMainForm.currentDiagramFlag = Diagram.NullObject;
            }
            else
            {
                Invalidate(/*new Rectangle(Math.Min(startPoint.X, pt.X), Math.Min(startPoint.Y, pt.Y), Math.Abs(startPoint.X - pt.X), Math.Abs(startPoint.Y - pt.Y))*/);
            }
        }
        protected void RenameProcess()
        {
            foreach (OriginalDiagram diag in diagramList)
            {
                //确保名字不为空
                if (diag.NameBox.Text != "")
                {
                    diag.Name = diag.NameBox.Text;
                    if (diag is HighOrderObjectDiagram)
                    {
                        HighOrderObjectDiagram obj = (HighOrderObjectDiagram)diag;
                        //修改高级图形对象的下层MainForm的名字，使名字同步
                        if (obj.HasModelForm)
                        {
                            obj.NewModelForm.Text = "ObjectModelForm: " + diag.Name + "/ Main";//Form名字同步
                        }
                        //this.preTreeNode.Nodes[obj.highDiagramNum + 1].Text = diag.Name;//treeView名字同步
                        //diag.treeNode.Text = diag.Name;//treeView名字同步
                    }
                    else if (diag is HighStateDiagram)
                    {
                        HighStateDiagram obj = (HighStateDiagram)diag;
                        //修改高级图形对象的下层MainForm的名字，使名字同步
                        if (obj.HasModelForm)
                        {
                            obj.NewModelForm.Text = "StateTransformModelForm: " + diag.Name + "/ Main";//Form名字同步
                        }
                        //diag.treeNode.Text = diag.Name;//treeView名字同步
                    }
                    else if (diag is InformationSourceDiagram)
                    {
                        InformationSourceDiagram obj = (InformationSourceDiagram)diag;
                        //修改高级图形对象的下层MainForm的名字，使名字同步
                        if (obj.HasModelForm)
                        {
                            obj.NewModelForm.Text = "InformationFlowerModelForm: " + diag.Name + "/ Main"; ;//Form名字同步
                        }
                        //diag.treeNode.Text = diag.Name;//treeView名字同步
                    }
                }
            }
         //   parentMainForm.treeView.Invalidate();
        }
        //清楚选中标志
        protected void ClearSelection()
        {
            foreach (OriginalDiagram diag in diagramList)
            {
                diag.IsSelected = false;
            }
            if (selectedDiagram != null)
            {
                Invalidate(new Rectangle(selectedDiagram.UpLeftpoint.X - 5, selectedDiagram.UpLeftpoint.Y - 5, selectedDiagram.Width + 11, selectedDiagram.Height + 11));
                selectedDiagram = null;
            }
            foreach (OriginalLine line in straightlineList)
            {
                line.IsSelected = false;
            }
            if (selectedLine != null)
            {
                Invalidate(/*new Rectangle(selectedLine.UpLeftpoint.X - 5, selectedLine.UpLeftpoint.Y - 5, selectedLine.Width + 11, selectedLine.Height + 11)*/);
                selectedLine = null;
            }
        }
        //显示弹出菜单
        protected void ShowContextMenuStrip(Point pt)
        {
            //Point pt = new Point(e.X, e.Y);
            bool diagramFlag = false;
            bool lineFlag = false;
            //右击是否在图形对象上
            foreach (OriginalDiagram dgr in diagramList)
            {
                Rectangle rect = new Rectangle(dgr.UpLeftpoint.X, dgr.UpLeftpoint.Y, dgr.Width, dgr.Height);
                if (selectedDiagram != null)
                {
                    diagramFlag = true;
                    break;
                }
            }
            //右击是否在直线上
            foreach (OriginalLine line in straightlineList)
            {
                if (line.IsSelectedLine(pt, line.StartPoint, line.EndPoint))
                {
                    lineFlag = true;
                    break;
                }
            }
            //右击在图形对象上，则弹出图形对象菜单
            if (diagramFlag)
            {
                this.ContextMenuStrip = diagramMenuStrip;
            }
            //右击在直线上，则弹出直线菜单
            else if (lineFlag)
            {
                this.ContextMenuStrip = lineMenuStrip;
            }
            //否则，弹出窗口菜单
            else
            {
                this.ContextMenuStrip = formMenuStrip;
                diagramFlag = false;
            }
            parentMainForm.currentDiagramFlag = Diagram.NullObject;
            Cursor = System.Windows.Forms.Cursors.Arrow;
        }
        #endregion

    }
}
